package org.j4me.deviceinfo.data;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;

import org.dom4j.Element;

public class Device {
	private Vector<Kvp> _properties = new Vector<Kvp>();
	private Vector<Jsr> _jsr = new Vector<Jsr>();
	private Vector<Key> _keys = new Vector<Key>();
	private Vector<MediaContent> _contents = new Vector<MediaContent>();
	private String _model;
	private String _brand;
	private String _operator;
	private int _hit = 1;
	private Date _created= new Date();

	public Device(Brand brand, Model m, Element elt) {
		_brand = brand.getName();
		_model = m.getName();
		_operator = elt.attributeValue("operator");
		try
		{
		_created = new Date(Long.parseLong(elt.attributeValue("created")));
		}catch(Exception e)
		{
			
		}
		_hit = Integer.parseInt(elt.attributeValue("hit"));
		Iterator it = elt.selectNodes("property").iterator();
		while (it.hasNext()) {
			_properties.add(new Kvp((Element) it.next()));
		}
		it = elt.selectNodes("jsr").iterator();
		while (it.hasNext()) {
			addJsr(new Jsr((Element) it.next()));
		}
		
		it = elt.selectNodes("key").iterator();
		while (it.hasNext()) {
			_keys.add(new Key((Element) it.next()));
		}
		it = elt.selectNodes("media").iterator();
		while (it.hasNext()) {
			_contents.add(new MediaContent((Element) it.next()));
		}
		
	}

	private void addJsr(Jsr jsr) {
		  for(int i=0; i < _jsr.size();i++)
			  if(jsr.compareTo(_jsr.elementAt(i))<= 0)
			  {
				  _jsr.insertElementAt(jsr, i);
				  return;
			  }
		  _jsr.add(jsr);
	}

	public Device(Vector<String[]> infos) {
		Enumeration<String[]> elts = infos.elements();
		while (elts.hasMoreElements()) {
			String[] kvp = (String[]) elts.nextElement();
			if ("brand".equals(kvp[0]))
				_brand = kvp[1];
			else if ("model".equals(kvp[0]))
				_model = kvp[1];
			else if ("operator".equals(kvp[0]))
				_operator = kvp[1];
			else if (kvp[0].startsWith("K["))
				_keys.add(new Key(kvp[0], kvp[1]));
			else if (kvp[0].startsWith("JSR_"))
				addJsr(new Jsr(kvp[0].substring("JSR_".length()), kvp[1]));
			else if (kvp[0].startsWith("MediaContent")) {
				StringTokenizer contents = new StringTokenizer(kvp[1], "{},");
				while (contents.hasMoreTokens()) {
					String content = contents.nextToken();
					String protocols = findValue(infos, "MediaProtocol_" + content);
					_contents.add(new MediaContent(content, protocols));
				}
			} else if (kvp[0].startsWith("MediaProtocol_"))
				continue;
			else if (kvp[0].startsWith("device."))
				continue;
			else
				_properties.add(new Kvp(kvp[0], kvp[1]));
		
		}
		_created = new Date();
	}

	private String findValue(Vector<String[]> infos, String key) {
		Enumeration<String[]> elt = infos.elements();
		while (elt.hasMoreElements()) {
			String[] kvp = (String[]) elt.nextElement();
			if (key.equals(kvp[0]))
				return kvp[1];
		}
		return null;
	}

	public boolean equals(Object obj) {
		if (obj instanceof Device) {
			Device model = (Device) obj;
			return (equals(_contents, model._contents) && equals(_jsr, model._jsr) && equals(_keys, model._keys) && equals(_properties, model._properties));
		}
		return super.equals(obj);
	}

	public void addXmlNode(Element root) {
		Element elt = root.addElement("device");
		elt.addAttribute("name", _model);
		elt.addAttribute("operator", _operator);
		elt.addAttribute("hit", "" + _hit);
		elt.addAttribute("created", "" + _created.getTime());
		for (int i = 0; i < _properties.size(); i++) {
			_properties.elementAt(i).toXml(elt.addElement("property"));
		}
		for (int i = 0; i < _keys.size(); i++) {
			_keys.elementAt(i).toXml(elt.addElement("key"));
		}
		for (int i = 0; i < _contents.size(); i++) {
			_contents.elementAt(i).toXml(elt.addElement("media"));
		}
		for (int i = 0; i < _jsr.size(); i++) {
			_jsr.elementAt(i).toXml(elt.addElement("jsr"));
		}
	}

	public String getBrand() {
		return _brand;
	}

	public String getModel() {
		return _model;
	}

	public String getOperator() {
		return _operator;
	}

	public int getHits() {
		return _hit;
	}

	public Enumeration<Key> getKeys() {
		return _keys.elements();
	}

	public Enumeration<Jsr> getJsr() {
		return _jsr.elements();
	}

	public Enumeration<MediaContent> getContents() {
		return _contents.elements();
	}

	public Enumeration<Kvp> getProperties() {
		return _properties.elements();
	}

	boolean equals(Vector v1, Vector v2) {
		if (v1 == v2)
			return true;
		if (v1 == null || v2 == null || v1.size() != v2.size())
			return false;
		for (int i = 0; i < v1.size(); i++) {
			if (!v2.contains(v1.elementAt(i)))
				return false;
		}
		return true;
	}

	public void increment() {
		_hit++;
	}

	public Date getCreated() {
		return _created;
	}
}
